package demo1;

public class BinaryLookup {
    public static void main(String[] args) {
        int[] arr = new int[]{5,7,7,7,7,8,8,10};
        int target = 7;
        int fineLeft = getLeftTarget(arr,target);
        System.out.println(fineLeft);
        int fineRight = getRightTarget(arr,target);
        System.out.println(fineRight);
    }
    public static int getLeftTarget(int[] arr,int target){
        int rs = -1;
        int left = 0;
        int right = arr.length-1;
        while (left < right){
            int index = (left + right) /2 ;
            if (arr[index] > target){
                right = index-1;
            } else if (arr[index] < target) {
                left = left + 1;
            }else {
                rs = index;
                right = index - 1;
            }

        }
        return rs;
    }

    public static int getRightTarget(int[] arr,int target){
        int rs = -1;
        int left = 0;
        int right = arr.length-1;
        while (left <= right){
            int index = (left + right) /2 ;
            if (arr[index] > target){
                right = index-1;
            } else if (arr[index] < target) {
                left = left + 1;
            }else {
                rs = index;
                left = index + 1;
            }

        }
        return rs;
    }
}
